﻿@using BXJG.Common;
@using ZLJ.App.Common.Customer;
@inherits OwningComponentBase
@inject OuAppService ouAppservice
@inject ISnackbar Snackbar
@inject IUnitOfWorkManager uowManager
@inject IObjectMapper mapper
<MudFocusTrap>

    <MudDialog>

        <TitleContent>
            <MudStack Row>
                <MudIcon Icon="@icon" />
                <MudText Typo="Typo.h6">@(Pattern.GetDescriptionString())部门</MudText>
            </MudStack>
        </TitleContent>
        <DialogContent>
            <EditForm EditContext="@editContext">
                <DataAnnotationsValidator />
                <MudTextField OnlyValidateIfDirty
                              ReadOnly="Pattern== FrmPattern.Look"
                              Label="*部门名称"
                              @bind-Value="@model.DisplayName"
                              For="@(()=>model.DisplayName)"
                              Clearable />

                <MudCheckBox LabelPosition="LabelPosition.Start"
                             ReadOnly="Pattern== FrmPattern.Look"
                             @bind-Checked="@model.IsActive"
                             Label="启用"
                             For="@(()=>model.IsActive)" />
            </EditForm>
        </DialogContent>
        <DialogActions>
            <MudHidden Breakpoint="Breakpoint.None" IsHidden="@(Pattern== FrmPattern.Look)">
                <MudButton Color="Color.Primary" Disabled="@(!success)" OnClick="Submit" StartIcon="@Icons.Outlined.Save" Variant="Variant.Filled">保存</MudButton>
            </MudHidden>
            <MudButton OnClick="Cancel" StartIcon="@Icons.Outlined.AssignmentReturn" Variant="Variant.Filled">关闭</MudButton>
        </DialogActions>

    </MudDialog>
</MudFocusTrap>

@code {
    /// <summary>
    /// 弹窗左上角的图表
    /// </summary>
    string icon
    {
        get
        {
            switch (Pattern)
            {
                case FrmPattern.Add:
                    return Icons.Outlined.Add;
                case FrmPattern.Edit:
                    return Icons.Outlined.Edit;
                default:
                    return Icons.Outlined.Shower;
            }
        }
    }
    [CascadingParameter] MudDialogInstance MudDialog { get; set; }

    [Parameter]
    public FrmPattern Pattern { get; set; }

    /*
    * dto本身就在列表存储了，这里传递过来最好
    * 若是复杂的处理场景，列表也应该传dto过来，表单根据需要再处理，起码dto已经提供很多有用的数据了
    */
    [Parameter]
    public OUDto Dto { get; set; }


    OUEditDto model = new OUEditDto();
    EditContext editContext;

    bool success => editContext.Validate();

    protected override void OnInitialized()
    {
        if (Pattern == FrmPattern.Edit)
            this.model = mapper.Map<OUEditDto>(Dto);
        editContext = new EditContext(model);
    }

    async Task Submit()
    {
        //有验证不过的时候，按钮无法点击的
        //if (editContext.Validate())
        //{
        //success = true;
        using (var uow = uowManager.Begin())
        {
            if (Pattern == FrmPattern.Add)
                await ouAppservice.CreateAsync(model);
            else
                await ouAppservice.UpdateAsync(model);

            await uow.CompleteAsync();
        }
        Snackbar.Add("保存成功！", Severity.Success);
        MudDialog.Close(DialogResult.Ok(true));
        //}
    }
    void Cancel() => MudDialog.Cancel();
}